package com.example.physicalexaminationsystem.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.example.physicalexaminationsystem.dto.ResponseDto;
import com.example.physicalexaminationsystem.mapper.DepartmentDao;
import com.example.physicalexaminationsystem.mapper.DoctorDao;
import com.example.physicalexaminationsystem.model.DepartmentEntity;
import com.example.physicalexaminationsystem.model.DoctorEntity;
import com.example.physicalexaminationsystem.vo.DoctorVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shiwenzhuo
 * 
 * @description
 */
@Component
public class DoctorService {
    @Resource
    private DoctorDao doctorDao;
    @Resource
    private DepartmentDao departmentDao;
    @Resource
    private DepartmentService departmentService;

    //添加医生信息
    public ResponseDto addDoctor(Map<String, Object> params) {
        Long departmentId = Long.valueOf(params.get("departmentId").toString());
        DepartmentEntity departmentEntity = departmentDao.selectById(departmentId);
        if (departmentEntity == null) {
            return ResponseDto.fail("科室不存在");
        }
        String name = params.get("name").toString();
        String gender = params.get("gender").toString();
        DateTime birthday = DateUtil.parse(params.get("birthday").toString());
        String info = params.get("info").toString();
        DoctorEntity doctorEntity = new DoctorEntity();
        doctorEntity.setDepartmentId(departmentId);
        doctorEntity.setName(name);
        doctorEntity.setGender(gender);
        doctorEntity.setBirthday(birthday);
        doctorEntity.setInfo(info);
        doctorDao.insert(doctorEntity);
        return ResponseDto.success();
    }

    //删除医生信息根据id
    public void deleteDoctorById(Long id) {
        doctorDao.deleteById(id);
    }

    //修改医生信息
    public ResponseDto updateDoctorById(Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Long departmentId = Long.valueOf(params.get("departmentId").toString());
        DepartmentEntity departmentEntity = departmentDao.selectById(departmentId);
        if (departmentEntity == null) {
            return ResponseDto.fail("科室不存在");
        }
        String name = params.get("name").toString();
        String gender = params.get("gender").toString();
        DateTime birthday = DateUtil.parse(params.get("birthday").toString());
        String info = params.get("info").toString();
        DoctorEntity doctorEntity = new DoctorEntity();
        doctorEntity.setId(id);
        doctorEntity.setDepartmentId(departmentId);
        doctorEntity.setName(name);
        doctorEntity.setGender(gender);
        doctorEntity.setBirthday(birthday);
        doctorEntity.setInfo(info);
        doctorDao.updateById(doctorEntity);
        return ResponseDto.success();
    }

    //获取医生信息
    public List<DoctorVo> getDoctorList() {
        return doctorDao.selectList(null).stream().map(this::DoctorEntityToDoctorVO).collect(Collectors.toList());
    }

    //DoctorEntity转DoctorVO
    public DoctorVo DoctorEntityToDoctorVO(DoctorEntity doctorEntity) {
        DoctorVo doctorVo = new DoctorVo();
        doctorVo.setDepartment(departmentService.DepartmentEntityToDepartmentVO(departmentDao.selectById(doctorEntity.getDepartmentId())));
        doctorVo.setName(doctorEntity.getName());
        doctorVo.setGender(doctorEntity.getGender());
        doctorVo.setAge(DateUtil.betweenYear(doctorEntity.getBirthday(), DateUtil.date(), true));
        doctorVo.setInfo(doctorEntity.getInfo());
        return doctorVo;
    }
}
